package ferp.android.widgets.buttons;

import android.content.Context;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.FrameLayout;

/**
 * User: igorgok
 * Date: 12/30/13 12:25 PM
 */
public abstract class TwoLayerButton extends FrameLayout
{
  public TwoLayerButton(Context context) {this(context, null, 0);}
  public TwoLayerButton(Context context, AttributeSet attrs) {this(context, attrs, 0);}
  public TwoLayerButton(Context context, AttributeSet attrs, int defStyle)
  {
    super(context, attrs, defStyle);

    setForegroundGravity(Gravity.CENTER);

    // button at the lower layer
    button = new Button(context);
    button.setBackgroundResource(getButtonBackgroundResource());

    // create the upper layer
    upper = onCreateUpperLayer(context, attrs);

    addView(button, new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT));
    addView(upper,
        new LayoutParams(getUpperLayerLayoutWidth(), getUpperLayerLayoutHeight(), getUpperLayerLayoutGravity()));
  }

  @Override
  public void setEnabled(boolean enabled)
  {
    super.setEnabled(enabled);

    button.setEnabled(enabled);
    upper.setEnabled(enabled);
  }

  @Override
  public void setOnClickListener(OnClickListener ocl) {button.setOnClickListener(ocl);}

  protected abstract View onCreateUpperLayer(Context context, AttributeSet attrs);
  protected abstract int getButtonBackgroundResource();

  protected int getUpperLayerLayoutWidth() {return ViewGroup.LayoutParams.MATCH_PARENT;}
  protected int getUpperLayerLayoutHeight() {return ViewGroup.LayoutParams.MATCH_PARENT;}
  protected int getUpperLayerLayoutGravity() {return Gravity.CENTER;}

  // button at lower layer
  private Button button;
  // upper view
  private View upper;
}
